package com.example.jh352160.surfingtv.mypage.customView.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.commodity_details.CommodityDetailsPage;

import java.util.List;

/**
 * Created by jh352160 on 2016/7/27
 */

public class CommodityRecyclerAdapter extends RecyclerView.Adapter<CommodityRecyclerAdapter.ViewHolder> {

    //    private int[] imageIds;
//    private String[] commoditis;
    private List<String> commonityName, commonityImageUrl, commonityFrom, commonityDetilUrl;
    private List<Boolean> commonityIsLike;
    private List<Integer> commonityId;
    private Context context;

//    public CommodityRecyclerAdapter(){
//        imageIds=new int[]{R.drawable.mypage_favorites_commodity,R.drawable.mypage_favorites_commodity,R.drawable.mypage_favorites_commodity,R.drawable.mypage_favorites_commodity,R.drawable.mypage_favorites_commodity,R.drawable.mypage_favorites_commodity};
//        commoditis=new String[]{"商品111111111111111","商品111111111111111","商品111111111111111","商品111111111111111","商品111111111111111","商品111111111111111"};
//    }

    public CommodityRecyclerAdapter(Context context, List<String> commonityName, List<String> commonityImageUrl, List<String> commonityFrom, List<String> commonityDetilUrl, List<Boolean> commonityIsLike, List<Integer> commonityId) {
        this.context = context;
        this.commonityName = commonityName;
        this.commonityImageUrl = commonityImageUrl;
        this.commonityFrom = commonityFrom;
        this.commonityDetilUrl = commonityDetilUrl;
        this.commonityIsLike = commonityIsLike;
        this.commonityId = commonityId;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.mypage_favorites_commodity_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Glide.with(context).load(commonityImageUrl.get(position)).into(holder.commodityImage);
        holder.commodityName.setText(commonityName.get(position));
        holder.commodityImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CommodityDetailsPage.class);
                intent.putExtra("from", commonityFrom.get(position));
                intent.putExtra("detilUrl", commonityDetilUrl.get(position));
                intent.putExtra("isLike", commonityIsLike.get(position));
                intent.putExtra("targetId", commonityId.get(position));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return commonityName.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView commodityImage;
        TextView commodityName;

        ViewHolder(View itemView) {
            super(itemView);
            commodityImage = (ImageView) itemView.findViewById(R.id.commodityImage);
            commodityName = (TextView) itemView.findViewById(R.id.commodityName);
        }
    }
}
